<?php
class Article_Controller extends Base_Controller
{
	public function index()
	{
		$this->template->title = Kohana::config('site.site_name');
		$pagination = new Pagination(array(
			'total_items'	=>	article::count_all(),
		));
		$this->template->content = View::factory('default')->set(array(
			'meta_info'	=>	article::find_all($pagination->items_per_page,$pagination->sql_offset),
			'pagination'=>	$pagination,
		));
	}

	public function detail($article_id)
	{
		$article_id = (int)$article_id;
		if($article_info = article::find($article_id))
		{
			$this->template->title = $article_info['meta']['title'];
			$this->template->content = View::factory('article_detail')->set(array(
				'meta'		=>	$article_info['meta'],
				'content'	=>	$article_info['content'],
				'comments'	=>	$article_info['comments'],
				'user'		=>	user::get_user_info($article_info['meta']['email']),
				'article_id'=>	$article_id,
			));
		}
		else
		{
			error_404();
		}
	}

	public function publish()
	{
		$form = $form_errors = array(
			'title'		=>	'',
			'content'	=>	'',
		);
		$this->template->title = '反馈';
		$this->template->content = View::factory('article_publish')->bind('form',$form)->bind('form_errors',$form_errors);
		if($_POST)
		{
			$data = $this->input->post();
			Article_Validation::publish($data);
			$form = $data->as_array();
			if(!access::verify_csrf())
			{
				message::error(Kohana::lang('error.csrf'));
			}
			else
			{
				if($data->validate())
				{
					$article_id = article::publish($form);
					url::redirect('article/detail/'.$article_id);
				}
				else
				{
					$form_errors = arr::overwrite($form_errors,$data->errors('article_error.publish'));
				}
			}
		}
	}

	public function create_comment($article_id)
	{
		access::require_login();
		$article_id = (int)$article_id;
		if($article_info = article::find($article_id) AND trim($this->input->post('content'))!='')
		{
			if(!access::verify_csrf())
			{
				message::error('error.csrf');
			}
			else
			{
				$comment_path = Kohana::config('core.comment_data_path');
				$comment_dir = $comment_path.$article_id;
				file_exists($comment_dir) OR mkdir($comment_dir);
				$comment_num = (glob($comment_dir.'/*.comment'))?glob($comment_dir.'/*.comment'):array();
				$comment_id = count($comment_num)+1;
				$meta = user::get_user()->email.'|'.time().PHP_EOL;
				$content = $this->input->post('content');
				file_put_contents($comment_dir.'/'.$comment_id.'.comment',$meta.$content);
			}
		}
		else
		{
			error_404();
		}
		url::redirect('article/detail/'.$article_id);
	}

	public static function delete_comment($article_id,$comment_id)
	{
		if(user::active()->login)
		{
			$comment_file = Kohana::config('core.comment_data_path').$article_id.'/'.$comment_id.'.comment';
			if(file_exists($comment_file))
			{
				$comment_info = comment::get_detail($comment_file);
				if($comment_info['email'] == user::get_user()->email OR user::active()->admin)
				{
					unlink($comment_file);
				}
			}
		}
		url::redirect('article/detail/'.$article_id);
	}

	public function edit($article_id)
	{
		$article_id = (int)$article_id;
		access::require_login();
		$meta = article::get_meta($article_id);
		if(user::get_user()->email == $meta['email'] OR user::active()->admin)
		{
			$this->template->title = '编辑反馈';
			$form = array('title'=>$meta['title'],'content'=>article::get_content($article_id),'type'=>$meta['type']);
			$form_errors = array('title'=>'','content'=>'');
			$this->template->content = View::factory('article_edit')->bind('form',$form)->bind('form_errors',$form_errors);
			$this->template->content->article_id = $article_id;
			if($_POST)
			{
				$data = $this->input->post();
				Article_Validation::publish($data);
				if($data->validate())
				{
					$data = $data->as_array();
					article::update_meta($article_id,array('title'=>$data['title'],'type'=>$data['type']));
					article::update_content($article_id,$data['content']);
					url::redirect('article/detail/'.$article_id);
				}
				else
				{
					$form_errors = arr::overwrite($form_errors,$data->errors('article_error.publish'));
				}
			}
		}
	}

	public function vote($article_id)
	{
		$article_id = (int)$article_id;
		access::require_login();
		if(!user::has_voted($article_id))
		{
			user::vote($article_id);
			ajax::response('success');
		}
		ajax::response('error');
	}

	public function change_status($article_id,$status)
	{
		$article_id = (int)$article_id;
		$status = (int)$status;
		article::change_status($article_id,$status);
		url::redirect('article/detail/'.$article_id);
	}

	public function delete($article_id)
	{
		$article_id = (int)$article_id;
		$meta_info = article::get_meta($article_id);
		if(user::active()->login)
		{
			if(user::active()->admin OR user::get_user()->email = $meta['email'])
			{
				$article_path = Kohana::config('core.article_data_path');
				unlink($article_path.$article_id.'.meta');
				unlink($article_path.$article_id.'.content');
				@unlink($article_path.$article_id.'.vote');
				$comment_path = Kohana::config('core.comment_data_path');
				rmdirr($comment_path.$article_id);
			}
		}
		url::redirect();
	}
}
